package com.foreverwin.jzymes.report.gr.controller;

import com.foreverwin.jzymes.common.constant.Constants;
import com.foreverwin.jzymes.common.service.ICustomFieldsAdviceService;
import com.foreverwin.jzymes.common.util.CommonMethods;
import com.foreverwin.jzymes.framework.base.AjaxResult;
import com.foreverwin.jzymes.framework.base.BaseController;
import com.foreverwin.jzymes.framework.exception.BusinessException;
import com.foreverwin.jzymes.framework.util.ObjectMapperUtil;
import com.foreverwin.jzymes.production.cutter.service.IExportExcelService;
import com.foreverwin.jzymes.production.gr.domain.GoodsReceiptLog;
import com.foreverwin.jzymes.production.gr.mapper.GoodsReceiptLogMapper;
import com.foreverwin.jzymes.production.sfc.domain.SFC;
import com.foreverwin.jzymes.report.gr.service.IGoodsReceiptReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/report/gr")
public class GoodsReceiptReportController extends BaseController {
    @Autowired
    IGoodsReceiptReportService goodsReceiptReportService;
    @Autowired
    GoodsReceiptLogMapper goodsReceiptLogMapper;
    @Autowired
    IExportExcelService exportExcelService;
    @Autowired
    ICustomFieldsAdviceService customFieldsAdviceService;

    @RequestMapping("/GoodsReceiptReport")
    public AjaxResult GoodsReceiptReport( @RequestParam Map<String,Object> jsonMap ){
        try{
            jsonMap.put("site", CommonMethods.getSite());
            List< Map<String,Object> > logs = goodsReceiptReportService.findGoodsReceiptReport( jsonMap );
            return AjaxResult.returnSuccess().put( "value", logs );
        }catch (BusinessException e){
            return AjaxResult.returnError( e.getMessage() );
        }
    }

    /**
     * 导出excel
     * @param paramJson
     * @param response
     * @param request
     */
    @RequestMapping("/doexport")
    public void doexport(String  paramJson , HttpServletResponse response, HttpServletRequest request ){

        try{
            List< Map< String,Object > > list = ObjectMapperUtil.readValue( paramJson, ArrayList.class );

            String[] rowName = new String[]{"序号","机种","料号","物料简码","单位","工令","仓码","入库类型","入库状态","入库数量","创建日期","创建人员"};
            String title = "成品入库信息";
            String excel = "GR.xls";
            List<Object[]> dataList = new ArrayList<Object[]>();
            Object[] objs = null;
            for( int i = 0; i < list.size(); i++ ){
                GoodsReceiptLog goodsReceiptLog = goodsReceiptLogMapper.selectGoodsReceiptLogById( (String) list.get(i).get( "handle" ) );

                String code = customFieldsAdviceService.existCustomValue( goodsReceiptLog.getItemBo() , Constants.ITEM_CUSTOM_FIELD_ITEM_LABEL );
                SimpleDateFormat simpleDate = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );

                objs = new Object[rowName.length];
                objs[0] = i;
                objs[1] = goodsReceiptLog.getMachineType();
                objs[2] = goodsReceiptLog.getItemBo().split(",")[1];
                objs[3] = code;
                objs[4] = goodsReceiptLog.getUnit();
                objs[5] = goodsReceiptLog.getSfc();
                objs[6] = goodsReceiptLog.getStorageLocation();
                objs[7] = goodsReceiptLog.getGrType();
                objs[8] = goodsReceiptLog.getStatus();
                objs[9] = list.get(i).get( "qty" );
                objs[10] = simpleDate.format( goodsReceiptLog.getCreatedDateTime() );
                objs[11] = goodsReceiptLog.getCreateUserBo().split(",")[1];

                dataList.add(objs);
            }

            File file = exportExcelService.Export( dataList , title ,rowName ,excel );

            fileDownload( file,true,response,request );

        }catch (Exception e ) {
            throw  BusinessException.build(e.getMessage());
        }
    }
}
